import { observable, action, toJS,computed  } from 'mobx';
import { message } from 'antd';
import * as api from '../../apis/plan/planResultListApi'
import {  WeaTableNew } from 'comsMobx';
const { TableStore } = WeaTableNew;
import {idsAndNamesToArr,getWeekOfYear} from '../../util/Util'
import { WeaLocaleProvider,WeaTools} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
export default class PlanResultListStore {
    //带observable 的会重新渲染

    @observable loading = true; // 页面初始化的loading状态：数据加载成功前后前使用
    @observable tableStore = new TableStore(); // new table
    @observable showGroup=true;

    //当前选中tab的key 
    @observable tabSelectKey = '1'

    //日期选择
    @observable dateSelect={
        year:this.getLastMonthDate().year,month:this.getLastMonthDate().month,week:this.getLastMonthDate().week
    }

    //年列表
    @observable yearDatas = []

    //更多搜索条件
    @observable moreSearchValues={
        title:null,type:null,hrmIds:null,subType:'3',sub:null,depType:'3',dep:null,hrmnames:null,subnames:null,depnames:null
    }
    @observable statusOptions=[]
    @observable searchConditionItems=[]
    @computed  get hrmData() { //人力资源回显数据
        return idsAndNamesToArr(this.moreSearchValues.hrmIds, this.moreSearchValues.hrmnames);
    };
    @computed  get subcompanyData() { //分部回显数据
        return idsAndNamesToArr(this.moreSearchValues.sub, this.moreSearchValues.subnames);
    };
    @computed  get departmentData() { //部门回显数据
        return idsAndNamesToArr(this.moreSearchValues.dep, this.moreSearchValues.depnames);
    };

    //月份选择组件的刷新key
    @observable locationkey=new Date().getTime()

    //月份选择组件是否显示
    @observable monthWeekSelectComDisplayKey = ''

    //高级搜索变更事件
    @action
    onMoreSearchChange=(param,e)=>{
        this.moreSearchValues[param] = e
    }

    @action("搜索条件改变")
    onSearchChange = (params) => {
        for (let key in params) {
            this.moreSearchValues[key] = params[key];
        }
    }

    @action("重置高级搜索条件")
    resetMoreSearch=()=>{
        this.moreSearchValues={
            title:null,type:null,hrmIds:null,subType:'3',sub:null,depType:'3',dep:null,hrmnames:null,subnames:null,depnames:null
        }
    }

    //获取页面元素值
    @action
    searchResultList = params => {

        api.searchResultList(params).then(action((res)=>{
            if(res.api_status){
                this.tableStore.getDatas(res.datas)
                this.loading = false;
            }else{
                message.error(res.msg || getLabel('383746','请求失败') )
            }
        }))

    }

    //获取表格的元数据
    @action
    getResultListTable = params=>{

        api.getResultListTable(this.getRequestParams()).then(action((res)=>{
            if(res.api_status){
                if(params &&params.refresh){
                    this.tableStore.getDatas(res.datas,1)
                }else{
                    this.tableStore.getDatas(res.datas)
                }
                
                this.loading = false;
            }else{
                message.error(res.msg||getLabel('383746','请求失败') )
            }
        }))

    }
    //获取请求参数
    getRequestParams=()=>{
        let params = {}
        params.year = this.dateSelect.year
        params.dateType = this.tabSelectKey
        params.dateValue =  this.tabSelectKey == '1'? this.dateSelect.month:this.dateSelect.week
        params.status = this.moreSearchValues.status
        params.humId = this.moreSearchValues.hrmIds
        params.depId = this.moreSearchValues.dep
        params.subId = this.moreSearchValues.sub
        params.includeDepType = this.moreSearchValues.depType
        params.includeSubType = this.moreSearchValues.subType
        params.planName = this.moreSearchValues.title
        return params
    }
    

    //获取年份
    @action
    initYearDateSelectOptions = () => {
        let currentYear =parseInt(new Date().getFullYear());
        let yearList = []
        for(var i=currentYear;i>currentYear-6;i--){
            yearList.push({showname: i, key: i, selected: i==currentYear})
        }
        this.yearDatas = yearList
    }


      //状态下拉框
    @action('getOption')
    getStatusOption = (fn)=>{
        WeaTools.callApi('/api/workrelate/plan/getOption', 'GET', {type:3}).then(
            action(res=>{
                if(res.api_status){
                    this.statusOptions=res.data
                    
                }else{
                    message.error (res.msg || getLabel('383746','请求失败') );
                }
                if(fn){
                    fn()
                }
            })
        )
    }



    getLastMonthDate(){
        let now = new Date()
        const isJanuary = now.getMonth()==0
        let year = (isJanuary && this.tabSelectKey=='1')?now.getFullYear()-1:now.getFullYear()
        let month = isJanuary?12:now.getMonth()
        let week = getWeekOfYear()
        if(year == new Date().getFullYear()-1 &&week == 1 ){
            week = 52
        }
        return {
            year,
            month,
            week
        }
    }

}